package org.example.config;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.example.annotation.ProducerKafkaMessage;
import org.example.producer.LocalKafkaProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.Map;

/**
 * 通过代理实现发送消息
 * @author youshang
 * @date 2021/02/09 15:14
 **/
public class LocalProducerKafkaMessageMethodInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object proceed = invocation.proceed();
        ProducerKafkaMessage annotation = AnnotationUtils.getAnnotation(invocation.getMethod(), ProducerKafkaMessage.class);
        String topic = annotation.topic();
        LocalKafkaTemplate localKafkaTemplate = new LocalKafkaTemplate();
        localKafkaTemplate.send(topic,"",proceed);
        return proceed;
    }
}
